package org.whatever.aha.zjut.platform.service.competition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.dto.competition.CompetitionStageAwardDto;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStage;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageAward;
import org.whatever.aha.zjut.platform.mapper.competition.CompetitionStageAwardMapper;
import org.whatever.aha.zjut.platform.util.BeanUtil;
import org.whatever.aha.zjut.platform.vo.competition.CompetitionStageAwardVo;

import java.util.List;

/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/08/25 0:41
 */
@RequiredArgsConstructor
@Service
public class CompetitionStageAwardService {
    final CompetitionStageAwardMapper competitionStageAwardMapper;

    /**
     * 创建新的比赛阶段奖项信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int addCompStageAward(int stageId, CompetitionStageAwardDto competitionStageAwardDto){
        CompetitionStageAward competitionStageAward = new CompetitionStageAward();
        BeanUtils.copyProperties(competitionStageAwardDto, competitionStageAward);
        competitionStageAward.setStageId(stageId);
        return competitionStageAwardMapper.insert(competitionStageAward);
    }

    /**
     * 根据List创建新的比赛阶段奖项信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int addCompStageAwardByList(int stageId, List<CompetitionStageAwardDto> competitionStageAwardDtoList){
        competitionStageAwardDtoList.forEach(competitionStageAwardDto->this.addCompStageAward(stageId, competitionStageAwardDto));
        return competitionStageAwardDtoList.size();
    }

    /**
     * 删除比赛关键时间节点信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int delCompStageAward(int stageId){
//        if(competitionStageAwardMapper.selectCount(new QueryWrapper<CompetitionStageAward>().eq("stage_id",stageId))<1){
//            throw new AppException(ErrorCode.COMPETITION_STAGE_AWARD_LACK);
//        }
        return competitionStageAwardMapper.delete(new QueryWrapper<CompetitionStageAward>().eq("stage_id",stageId));
    }

    /**
     * 根据阶段Id获得所有竞赛奖励信息
     *
     * @param stageId
     * @return
     */
    public List<CompetitionStageAwardVo> getCompStageAwardListById( int stageId){
        List<CompetitionStageAward> competitionStageAwards = competitionStageAwardMapper.selectList(new QueryWrapper<CompetitionStageAward>().eq("stage_id", stageId).orderByAsc("award_order"));
        return BeanUtil.copyListProperties(competitionStageAwards , CompetitionStageAwardVo::new);
    }

    /**
     *  根据阶段Id、Vo更新竞赛获奖名额信息
     *
     * @param stageId
     * @param competitionStageAwardVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int updateCompStageAwardById(int stageId, CompetitionStageAwardVo competitionStageAwardVo){
        CompetitionStageAward competitionStageAward = new CompetitionStageAward();
        competitionStageAward.setStageId(stageId);
        BeanUtils.copyProperties(competitionStageAwardVo, competitionStageAward);
        return competitionStageAwardMapper.update(competitionStageAward, new QueryWrapper<CompetitionStageAward>().eq("stage_id",stageId));
    }
}
